# -*- coding:utf-8 -*-
import random
import requests
from requests.exceptions import ConnectionError
"""
利用proxy进行访问
"""


# 若开启headers，则调用前需信息参数headers

# 若开启proxy请求，则调用proxy_request()前需传入参数proxy_list
# 获取proxy_list如下：
# proxy_list = get_Proxies.temp_list()

proxy = None  # global全局变量时需先定义该变量


def proxy_request(start_url, method='GET', params=None, data=None, headers=None, proxy_list=None, count=1, max_count=5):
	"""返回res_doc_text"""
	print ('Crawling', start_url)
	print ('Tring Count', count)

	global proxy
	# 若proxy_list为True，则表开启proxy请求
	if proxy_list:
		proxy = list(random.choice(proxy_list))  # 放在此处，则每次调用`proxy_request()`都会更换proxy
	else:
		proxy = None

	if count >= max_count:  # 判断请求次数
		print ('Tried Too Many Counts')
		return None
	try:
		# 首先判断proxy是否为True
		if proxy:
			proxies = {
				'http': 'http://' + str(proxy[0])
			}
			print 'Using Proxy:：{0}'.format(proxies.get('http'))
		else:
			proxies = None

		res_doc = requests.request(method=method, url=start_url, params=params, data=data, headers=headers, proxies=proxies, allow_redirects=False)
		# 参数allow_redirects --表是否允许自动跳转

		# 判断响应码
		if res_doc.status_code == 200:
			# print res_doc.text
			return res_doc.text

		if res_doc.status_code == 302:  # 若响应码为`302`，则再次更换proxy进行请求
			print ('302')
			proxy = random.choice(proxy_list)
			if proxy:
				print ('Using Proxy:', proxy)
				return proxy_request(start_url, method=method, params=params, data=data, headers=headers, proxy_list=proxy_list)
			else:
				print ('Get Proxy Failed')
				return None

	except ConnectionError as e:
		print ('Error Occurred', e.args)
		proxy = random.choice(proxy_list)
		count += 1
		return proxy_request(start_url, method=method, params=params, data=data, headers=headers, proxy_list=proxy_list, count=count)


if __name__ == '__main__':
	"""测试调试"""
	from mod_Proxies import list_UserAgents
	from mod_Proxies import get_Proxies
	UserAgent = random.choice(list_UserAgents.UserAgent)
	headers = {
		'Accept': 'application/json, text/javascript, */*; q=0.01',
		'Accept-Encoding': 'gzip, deflate, br',
		'Accept-Language': 'zh-CN,zh;q=0.9',
		'Cache-Control': 'no-cache',
		'Connection': 'keep-alive',
		'Content-Length': '25',
		'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
		# 'Cookie': 'user_trace_token=20180118150844-6bdeb2fa-fc1e-11e7-a854-525400f775ce; LGUID=20180118150844-6bdeb67e-fc1e-11e7-a854-525400f775ce; index_location_city=%E5%85%A8%E5%9B%BD; TG-TRACK-CODE=index_search; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1514212709,1514900095,1516259322,1516340995; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1516347901; JSESSIONID=ABAAABAABEEAAJA70AD042495453200E319666EC9BCD985; _gid=GA1.2.1853430185.1516259322; _gat=1; _ga=GA1.2.1670913001.1516259322; LGSID=20180119154501-a7c518ca-fcec-11e7-a52b-5254005c3644; PRE_UTM=; PRE_HOST=; PRE_SITE=https%3A%2F%2Fwww.lagou.com%2F; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Fjobs%2Flist_python%3FlabelWords%3D%26fromSearch%3Dtrue%26suginput%3D; LGRID=20180119154501-a7c51a1f-fcec-11e7-a52b-5254005c3644; SEARCH_ID=b9a2588e1df94a008622f10c471e4dac',
		'Host': 'www.lagou.com',
		'Origin': 'https://www.lagou.com',
		'Pragma': 'no-cache',
		'Referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=',
		'User-Agent': UserAgent,
		'X-Anit-Forge-Code': '0',
		'X-Anit-Forge-Token': None,
		'X-Requested-With': 'XMLHttpRequest',
	}

	start_url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false&isSchoolJob=0'
	proxy_list = get_Proxies.temp_list()  # 开启proxy请求
	pns = 5
	kd = 'Python'
	for pn in range(1, pns):
		form = {
			'first': 'true',
			'pn': pn,
			'kd': kd,
		}
		proxy_request(start_url, method='POST', data=form, headers=headers, proxy_list=proxy_list)
